package ynu.edu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.controller.LogicController;
import ynu.edu.dao.IShopDao;
import ynu.edu.dto.*;
import ynu.edu.entity.*;
import ynu.edu.service.ShopService;
import ynu.edu.utils.Result;
import ynu.edu.utils.inna.AuthAccess;
import ynu.edu.utils.inna.AuthPermission;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static ynu.edu.controller.CustomerController.getResult;


@Api(tags = "门店实体的控制器")
@RestController
@RequestMapping("/shop")
public class ShopController extends LogicController<ShopService, IShopDao, Shop,Long> {
    public ShopController(@Autowired ShopService ls) {
        super(ls);
    }
    @Resource
    private ModelMapper modelMapper;

    @AuthAccess
    @ApiOperation("获取门店信息")
    @GetMapping("/getShopById")
    @Transactional
    public Result getShopById(@RequestParam("id")Long shopId){
        return Result.success(modelMapper.map(getService().getShopById(shopId),ShopDTO.class));
    }

    @AuthAccess
    @ApiOperation("顾客页面：获取首页全部门店信息")
    @GetMapping("/getAllShops")
    @Transactional
    public Result getAllShops(){
        List<Shop> shops = getService().getAllShops();
        return Result.success(shops.stream().map(
                shop -> modelMapper.map(shop,ShopDTO.class)).collect(Collectors.toList()));
    }

    @AuthAccess
    @ApiOperation("顾客页面：获取门店的已上架商品")
    @GetMapping("/getShopItemByShop/{shopId}")
    @Transactional
    public Result getShopItemByShop(@PathVariable("shopId")Long shopId){
        List<ShopItem> shopItems = getService().listMyShopItemsIsOnShelves(shopId);
        return Result.success(shopItems.stream().map(
                shopItem -> modelMapper.map(
                        shopItem,ShopItemDTO.class)).collect(Collectors.toList()));
    }

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("商家页面：获取我的门店商品")
    @GetMapping("/listMyShopItems")
    @Transactional
    public Result listMyShopItems(@RequestParam("id") Long shopId){
        List<ShopItem> list = getService().listMyShopItems(shopId);
        return Result.success(list.stream().map(shopItem -> modelMapper.map(shopItem, ShopItemDTO.class)).
                collect(Collectors.toList()));
    }

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("在门店上架一件商品，也就是新建一个门店商品")
    @PostMapping ("/createShopItem")
    @Transactional
    public Result createShopItem(
            @RequestParam("itemId") Long itemId,
            @RequestParam("shopId") Long shopId,
            @RequestParam("price") Double price
    ){
        ShopItem item = getService().createShopItem(shopId, itemId, price);
        return Result.success(modelMapper.map(item, ShopItemDTO.class));
    }

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("上架/下架(已存在的门店商品)")
    @PostMapping ("/updateShopItemState")
    @Transactional
    public Result updateShopItem(
            @RequestParam("shopId") Long shopId,
            @RequestParam("shopItemId") Long shopItemId
    ){
        ShopItem updateShopItem = getService().updateShopItem(shopId, shopItemId);
        if (Objects.isNull(updateShopItem)){
            return Result.error("门店商品不存在");
        }
        return Result.success(modelMapper.map(updateShopItem, ShopItemDTO.class));
    }

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("获取门店已经支付订单")
    @GetMapping ("/getMyShopOrder")
    @Transactional
    public Result getMyShopOrders(
            @RequestParam("shopId") Long shopId,
            @RequestParam("orderState") Integer orderState
    ){
        List<Order> orders = getService().getMyShopOrder(shopId,orderState);
        List<OrderDTO> orderDTOS =new ArrayList<>();
        for (Order order : orders) {
            List<LineItemDTO> lineItemDTOS = order.getLineItems().stream().map(
                    lineItem -> modelMapper.map(lineItem, LineItemDTO.class)).toList();
            OrderDTO orderDTO = modelMapper.map(order, OrderDTO.class);
            orderDTO.setLineItemDTOS(lineItemDTOS);
            orderDTOS.add(orderDTO);
        }
        return Result.success(orderDTOS);
    }

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("修改订单状态")
    @PostMapping("/updateOrderState")
    @Transactional
    public Result updateOrderState(
            @RequestParam("orderId") Long orderId,
            @RequestParam("shopId") Long shopId,
            @RequestParam("orderState") Integer orderState
    ){
        Order order = getService().updateOrderState(shopId, orderId, orderState);
        return getResult(modelMapper, order);
    }
}
